package XML与JSON.使用SAX;

import java.io.File;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class 解析XML文档 {

   public static void main(String[] args) {

      try {
         //File inputFile = new File("src\\main\\java\\XML与JSON\\使用SAX\\input.txt");
         SAXParserFactory factory = SAXParserFactory.newInstance();
         SAXParser saxParser = factory.newSAXParser();
         UserHandler userhandler = new UserHandler();
         //saxParser.parse(inputFile, userhandler);     
         saxParser.parse(解析XML文档.class.getResourceAsStream("input.txt"), userhandler);//    
      } catch (Exception e) {
         e.printStackTrace();
      }
   }   
}

class UserHandler extends DefaultHandler {

	boolean bClass=false;
   boolean bFirstName = false;
   boolean bLastName = false;
   boolean bNickName = false;
   boolean bMarks = false;

   //在元素的开头调用
   @Override
   public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
      
   	String str=qName.toLowerCase();
   	switch (str) {
   		case "class":
   			System.out.println("Start Element :"+str);//+"    "+attributes.getQName(0)+"="+attributes.getValue(0));
   			bClass=true;
   			break;
   		case "student":
   			String rollNo = attributes.getValue("rollno");
            System.out.println("Start Element :"+str+"  Roll No : " + rollNo);
            break;
   		case "firstname":
   			bFirstName=true;
   			break;
   		case "lastname":
   			bLastName=true;
   			break;
   		case "nickname":
   			bNickName=true;
   			break;
   		case "marks":
   			bMarks=true;
   			break;
   	}
//      if (qName.equalsIgnoreCase("student")) {
//         String rollNo = attributes.getValue("rollno");
//         System.out.println("Roll No : " + rollNo);
//      } else if (qName.equalsIgnoreCase("firstname")) {
//         bFirstName = true;
//      } else if (qName.equalsIgnoreCase("lastname")) {
//         bLastName = true;
//      } else if (qName.equalsIgnoreCase("nickname")) {
//         bNickName = true;
//      }
//      else if (qName.equalsIgnoreCase("marks")) {
//         bMarks = true;
//      }
   }

   //在元素末尾调用
   @Override
   public void endElement(String uri, String localName, String qName) throws SAXException {
      //在已知元素名称的情况下，也可自动获取元素名称
      if ((qName.equalsIgnoreCase("student") | (qName.equalsIgnoreCase("class")))) {
         System.out.println("End Element :" + qName);
      } 
   }

   //遇到字符数据时调用
   @Override
   public void characters(char ch[], int start, int length) throws SAXException {
//   	if (bClass) {
//   		for (char c:ch) {
//   			System.out.print(c);
//   		}
//   		bClass=false;
//   	}
      if (bFirstName) {
         System.out.println("First Name: " + new String(ch, start, length));
         bFirstName = false;
      } else if (bLastName) {
         System.out.println("Last Name: " + new String(ch, start, length));
         bLastName = false;
      } else if (bNickName) {
         System.out.println("Nick Name: " + new String(ch, start, length));
         bNickName = false;
      } else if (bMarks) {
         System.out.println("Marks: " + new String(ch, start, length));
         bMarks = false;
      } else if (bClass) {
      		//System.out.println("class: " + new String(ch, start, length));
         bClass = false;
      }
   }
}

/*
<?xml version = "1.0"?>
<class>
   <student rollno = "393">
      <firstname>dinkar</firstname>
      <lastname>kad</lastname>
      <nickname>dinkar</nickname>
      <marks>85</marks>
   </student>
   
   <student rollno = "493">
      <firstname>Vaneet</firstname>
      <lastname>Gupta</lastname>
      <nickname>vinni</nickname>
      <marks>95</marks>
   </student>
   
   <student rollno = "593">
      <firstname>jasvir</firstname>
      <lastname>singn</lastname>
      <nickname>jazz</nickname>
      <marks>90</marks>
   </student>
</class>
*/